import Vue from 'vue'
import Vuex from 'vuex'
import Cookie from 'js-cookie'
import axios from '../utils/http'
import Router from '../router'
import API from '@/config/api'
import {Message} from 'element-ui'

// 定义全局API域名
const api = API

Vue.use(Vuex)

export default new Vuex.Store({
  namespcaed: true,
  state: {
    duration: 1500,
    cPath: Cookie.get('cPath') || '', // 当前菜单的index
    isCollapse: Boolean(Cookie.get('isCollapse')) || false, // 左侧菜单是否折叠
    isLoading: false,
    user: {
      uid: '',
      name: '',
      storeId: ''
    },
    permission: []
  },
  getters: {
    username: state => { // 返回当前用户名
      return decodeURIComponent(state.user.name)
    }
  },
  mutations: {
    setLoading (state, data) { // 设置页面加载状态
      if (data) {
        state.isLoading = true
      } else {
        state.isLoading = false
      }
    },
    setCollapse (state, data) {
      if (data) {
        state.isCollapse = true
        Cookie.set('isCollapse', true)
      } else {
        state.isCollapse = false
        Cookie.remove('isCollapse')
      }
    },
    setCPath (state, data) { // 设置当前菜单
      if (data) {
        state.cPath = data
        Cookie.set('cPath', data)
      } else {
        state.cPath = ''
        Cookie.set('cPath', '')
      }
    },
    setToken (state, data) { // 设置token
      if (data) {
        state.token = data
        Cookie.set('token', data)
      } else {
        state.token = ''
        Cookie.remove('token')
      }
    },
    setUid (state, data) { // 设置userid
      if (data) {
        state.user.uid = data
        Cookie.set('uid', data)
      } else {
        state.user.uid = ''
        Cookie.remove('uid')
      }
    },
    setUsername (state, data) { // 设置用户名
      if (data) {
        state.user.name = data
        Cookie.set('name', data)
      } else {
        state.user.name = ''
        Cookie.remove('name')
      }
    },
    setStoreId (state, data) { // 设置用户名
      if (data) {
        state.user.storeId = data
        Cookie.set('storeId', data)
      } else {
        state.user.storeId = ''
        Cookie.remove('storeId')
      }
    },
    setPermission (state, data) { // 设置用户权限
      if (data) {
        state.permission = data
      } else {
        state.permission = []
      }
    }
  },
  actions: {
    login ({commit, rootState}, userInfo) { // 登录
      return new Promise((resolve, reject) => {
        axios.post(api + '/user/login', userInfo).then(res => {
          if (res.responseCode === '0000') {
            commit('setUid', res.data.userId)
            commit('setStoreId', res.data.storeId)
            commit('setUsername', res.data.userName)
            commit('setToken', res.data.token)
          }
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    logout ({commit, rootState}) { // 退出登录
      return new Promise((resolve, reject) => {
        axios.post(api + '/user/logout', {}).then(res => {
          commit('setUid', '')
          commit('setStoreId', '')
          commit('setUsername', '')
          commit('setToken', '')
          Message({
            type: 'success',
            duration: rootState.duration,
            message: '已退出登录，即将跳转到登陆页面...',
            onClose: () => {
              Router.push('/login')
            }
          })
        })
      })
    },
    relogin ({commit}) { // 重新登录
      return new Promise(resolve => {
        commit('setUid', Cookie.get('uid'))
        commit('setStoreId', Cookie.get('storeId'))
        commit('setUsername', Cookie.get('name'))
        commit('setToken', Cookie.get('token'))
        resolve()
      })
    },
    getPermission ({commit, rootState}) { // 获取用户权限
      return new Promise((resolve, reject) => {
        axios.post(api + '/user/getMenuList', {}).then(res => {
          if (res.responseCode === '0000') {
            commit('setPermission', res.data)
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
})
